package sk.stu.fei.tomsteam;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class AddAgentDialog extends JDialog {

	private static final long serialVersionUID = 1L;
	private TreeInPanel treeInPanel;
	private String nameText;
	private String ipAddressText;
	private String portText;
	private String communityText;
	private JTextField name;
	private JTextField ipAddress;
	private JTextField port;
	private JTextField community;
	private JLabel nameLabel;
	private JLabel ipAddressLabel;
	private JLabel portLabel;
	private JLabel communityLabel;
	private JPanel textFieldsPanel;
	private JPanel buttonsPanel;
	private Container contentPane;
	private JFrame uiFrame;
	
	public AddAgentDialog(TreeInPanel treeInPanel, JFrame uiFrame) {//
		//Spring Layout
		this.treeInPanel = treeInPanel;
		this.uiFrame = uiFrame;
		initUI();
	}
	
	public final void initUI() {
		//setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
        //add(Box.createRigidArea(new Dimension(0, 10)));
		setLayout(new BorderLayout());
		contentPane = getContentPane();
		textFieldsPanel = new JPanel();
		buttonsPanel = new JPanel();
		contentPane.add(new JLabel("Please type agent parameters"), BorderLayout.PAGE_START);
        contentPane.add(textFieldsPanel, BorderLayout.CENTER);
        contentPane.add(buttonsPanel, BorderLayout.PAGE_END);

        
        
        name = new JTextField(20);
        ipAddress = new JTextField(20);
        port = new JTextField(20);
        community = new JTextField(20);
        nameLabel = new JLabel("Name: ");
        ipAddressLabel = new JLabel("IP Address: ");
        portLabel = new JLabel("Port: ");
        communityLabel = new JLabel("Community: ");
        textFieldsPanel.add(nameLabel);
        textFieldsPanel.add(name);
        textFieldsPanel.add(ipAddressLabel);
        textFieldsPanel.add(ipAddress);
        textFieldsPanel.add(portLabel);
        textFieldsPanel.add(port);
        textFieldsPanel.add(communityLabel);
        textFieldsPanel.add(community);
        
        /*name.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent event) {//po stlaceni enteru, my vsak chceme po stlaceni buttonu
        		nameText = name.getText();
        	}
        });*/
        
        JButton saveButton = new JButton("Save");
        saveButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
            	nameText = name.getText();
            	ipAddressText = ipAddress.getText();
            	portText = port.getText();
            	communityText = community.getText();
            	
            	Agent agent = new Agent(nameText, ipAddressText, portText, communityText);	//creating new Agent object
            	
            	Agent.listOfAgents.add(agent);	//filling listOfAgent with created Agent objects
            	treeInPanel.addObject(agent);	//adding agent icon to the tree
                dispose();
            }
        });
        
        JButton closeButton = new JButton("Close");
        closeButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {
                dispose();
            }
        });
        
        saveButton.setBounds(35, 120, 100, 25);
        buttonsPanel.add(saveButton);
        closeButton.setBounds(145, 120, 100, 25);
        buttonsPanel.add(closeButton);
        
        setModalityType(ModalityType.APPLICATION_MODAL);
        setTitle("Add SNMP agent");
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(uiFrame);
        setSize(280, 200);
	}
	

}
